package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.controller.admin.SetmealController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 老杨
 * @version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        //添加套餐
            //1.向套餐表添加
        setmealMapper.addSetmeal(setmeal);
            //2.向套餐和菜品 的关系表添加 多条数据
                //设置一下 套餐id
        for (SetmealDish setmealDish:setmealDishes){
            setmealDish.setSetmealId(setmeal.getId());
        }
        setmealDishMapper.addSetmeal(setmealDishes);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 修改套餐
            //1.修改套餐表
        setmealMapper.update(setmeal);

            //2.修改 套餐菜品关联表
                //2.1 如果菜品集合不为null，就删除原有套餐的关联菜品
            if (setmealDishes != null && setmealDishes.size() > 0){
                setmealDishMapper.delete(setmeal.getId());
                //添加这个修改后的 套餐 关联 菜品
                setmealDishMapper.addSetmeal(setmealDishes);
            }

    }


    @Override
    public Setmeal listById(Long id) {
        Setmeal setmeal = setmealMapper.listById(id);
        return setmeal;
    }

    @Override
    public void delete(List<Long> ids) {
        //起售中的 套餐不能删除
        for (Long id :ids){
            Setmeal select = setmealMapper.select(id);
            if (select.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.delete(ids);
    }

    @Override
    public void openOrdisable(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status).build();

        //启用 -- 对应菜品也起售了才行
        if (status == 1){
            //判断 套餐 关联的菜品是否全部 起售
            List<Dish> dishList = setmealDishMapper.getDishIdBySetmealId(id);
            for (Dish dish : dishList){
                if (dish.getStatus() == 0){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }

        }

        setmealMapper.update(setmeal);

    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
